# -*- coding: utf-8 -*-
'''
# =============================================================================
#      FileName: ads_manager.py
#        Author: Liu Ruichao
#         Email: ruichao.liu@duomi.com
#      HomePage: http://www.meelive.cn
#       Version: 0.0.1
#    LastChange: 2016-03-03 14:29:40
#       History:
# =============================================================================
'''
import re
import jieba
import string
from ConfigParser import ConfigParser
from dm_py_util import DEBUG_LOG, WARN_LOG, ERROR_LOG, SET_LOG_KEY, BUSINESS_LOG
pattern = re.compile(ur'([\u4e00-\u9fa5a-zA-Z0-9]+)')
numPattern = re.compile(ur'([0-9]+)')
class AdsManager(object):
    def init(self,config):
        try:
            cfg = ConfigParser()
            cfg.read(config)
        except Exception as e:
            ERROR_LOG('init AdsManager failed',e)
            exit(-1)
        try:
            self._nick_censor_word={}
            self._desc_censor_word={}
            nick_censor_word_filename = cfg.get('AdsManager','nick_censorword')
            desc_censor_word_filename = cfg.get('AdsManager','desc_censorword')
            self._init_censor_word(nick_censor_word_filename,desc_censor_word_filename)
        except Exception as e:
            ERROR_LOG('init censorWord failed',e)
            exit(-1)
        try:
            slice_word_filename = cfg.get('AdsManager','sliceword')
            jieba.load_userdict(slice_word_filename)
        except Exception as e:
            ERROR_LOG('init sliceWord failed',e)
            exit(-1)

            
    def _init_censor_word(self,filename_nick,filename_desc):
        with open(filename_nick,'r') as f:
            while True:
                line = f.readline()
                if not line:
                    break
                if not self._nick_censor_word.get(str(line).strip(),None):
                    self._nick_censor_word[str(line).strip().decode('utf-8')]=True
        with open(filename_desc,'r') as f:
            while True:
                line = f.readline()
                if not line:
                    break
                if not self._desc_censor_word.get(str(line).strip(),None):
                    self._desc_censor_word[str(line).strip().decode('utf-8')] = True



    def _character_filter(self,source_str):
        if len(source_str) == 0:
            return None
        match = pattern.findall(str(source_str).decode('utf-8'))
        if not match:
            return None
        return ''.join(match)
    
    def _find(self,source,word,check):
        replace=[]
        if not word or len(word) == 0:
            return None

        sentence = self._character_filter(word)

        if not sentence or len(sentence) == 0:
            return None

        slice_word = jieba.lcut(sentence)
        word_sort = sorted(slice_word)
        for s_word in word_sort:
            if check == True:
                if len(s_word) >= 9:
                    match = numPattern.findall(s_word)
                    if not match:
                        continue
                    try:
                        if string.atoi(''.join(match)) > 100000000:
                            replace.append(s_word)
                    except Exception:
                        pass
            if source == 'nick':
                if self._nick_censor_word.get(s_word,False):
                    replace.append(s_word)
            elif source == 'desc':
                if self._desc_censor_word.get(s_word,False):
                    replace.append(s_word)

        return replace

    def check(self,source,word,check=True):
        replace = self._find(source,word,check)
        if len(replace) == 0:
            return False
        else:
            return True
    def replace(self,source,word,check=True):
        replace = self._find(source,word,check)
        word = word.decode('utf-8')
        for b_word in replace:
            part_str = len(b_word)*'*'
            word = word.replace(b_word,part_str)
        return word

    def _convert_pinyin(self,word):
        return word


if __name__ == '__main__':
    a = AdsManager()
    a.init('./ads_manager.ini')
    if not a:
        print 'None'
    with open('./nick.txt','r') as f:
        count = 0
        fcount = 0
        while True:
            line = f.readline()
            if not line:
                break
            fin = a.check('desc',line,False)
            if fin:
                print line
        #     if fin == True:
        #         count += 1
        #     else:
        #         print line
        #         fcount += 1
        # print count*1.0/(fcount+count)
